package com.xxz.dao;
import com.xxz.pojo.Department;
import com.xxz.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;
    @Autowired
    private DepartmentDao departmentDao;
    static {
        employees = new HashMap<Integer, Employee>();//创建一个员工表
        employees.put(1, new Employee(1, "张三", "1234@qq.com", 1, new Department(1, "技术部")));
        employees.put(2, new Employee(2, "李四", "1345@qq.com", 1, new Department(2, "市场部")));
        employees.put(3, new Employee(3, "王五", "5665@qq.com", 0, new Department(3, "调研部")));
        employees.put(4, new Employee(4, "孙六", "7688@qq.com", 1, new Department(4, "后勤部")));
        employees.put(5, new Employee(5, "七七", "8089@qq.com", 1, new Department(5, "运营部")));
    }
    //主键自增
    private static Integer initialID = 6;
    //增加一个员工
    //  这里必须这样  才保证  增加员工的实现是  可以一直增加 而不是只增加一个人
    public void addEmployee(Employee employee) {
        if (employee.getId() == null)
            employee.setId(initialID++);
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }
    //查询全部员工 遍历用
    public Collection<Employee> getEmployeeAll(){
        return employees.values();
    }
    //通过Id查询员工  这是用来修改页面用
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }
    //删除员工
    public void deleteEmployee(Integer id){
        employees.remove(id);
    }
}